/**
 * Created by pactera on 2018/5/23.
 */

var vm = new Vue({
    el:"#role",
    data:{
        selectRows:[],
        roleColumn:[
            {type: 'selection', width: 60,align: 'center'},
            {title: '角色',key: 'name'},
            {title: '描述',key: 'description'},
            {title: '创建时间',key: 'createdDate'},
            {title: '修改时间',key: 'updateDate'}
        ],
        roles:[],
        formItem: {},
        showList: false,
        totalElements: 0,
        pageSize: 8,
        page:0,
        menuTree: [],
        roleIds:[]


    },
    mounted:function () {
        var _this = this;
        $.get("/role?page=0&size="+this.pageSize, function(r){
            _this.roles = r.data.content;
            _this.totalElements = r.data.totalElements;
        });
    },
    methods:{
        changePage:function (index) {
            this.page = index-1;
            this.getRoles();
        },
        onSelect: function (selection,row) {
            this.selectRows = selection;
        },
        onSelectCancel: function (selection,row) {
            this.selectRows = selection;
        },
        addMenu: function () {
            this.showList = true
            this.loadTreeData(null);
            this.selectRows=[];
        },
        saveOrUpdate: function () {
            var _this = vm;

            var checkNodes = this.$refs.tree.getCheckedNodes();

            debugger;
            // nodes.push(checkNodes);
            this.onCheckChange(checkNodes);
            this.formItem.roleMenus = this.roleIds;
            $.ajax({
                url: '/role/save',
                type: 'post',
                contentType: "application/json",
                data: JSON.stringify(_this.formItem),
                success: function(result) {
                    if(result.code == 0){
                        _this.showList = false;
                        _this.getRoles(0);
                    }
                }
            });
        },
        getRoles: function () {
            var _this = vm;
            this.formItem={};
            $.get("/role?page="+this.page+"&size="+this.pageSize, function(r){
                _this.roles = r.data.content;
                _this.totalElements = r.data.totalElements;
            });
        },
        deleteRoles: function () {
            var _this = vm;
            $.ajax({
                url: '/role/delete',
                type: 'post',
                contentType: "application/json",
                data: JSON.stringify(_this.selectRows),
                success: function(result) {
                    if(result.code == 0){
                        _this.getRoles();
                        // this.$Notice.warning({
                        //     title: '删除成功！'
                        // });
                    }
                }
            });
        },
        updateMenu: function () {
            var rows = this.selectRows;
            if(rows && rows.length > 1){
                this.$Notice.warning({
                    title: '只能选择一条数据！'
                });
            }else if (rows && rows.length == 1){
                this.loadTreeData(this.selectRows[0] .id);
                this.showList = true;
                this.formItem = this.selectRows[0];
            }else{
                this.$Notice.warning({
                    title: '请选择需要修改的数据！'
                });
            }
        },
        loadTreeData: function (id) {
            // var id = null;
            // if(this.selectRows && this.selectRows[0]){
            //         id =this.selectRows[0] .id
            // }
            $.get("/menu/trees/"+id, function(r){
                vm.menuTree = r.data;
            });
        },
        onCheckChange: function (data) {
            var nodes = new Array;
            debugger
            for (var item in data) {
                nodes.push(data[item].id);
                if(data[item].children){
                    this.onCheckChange(data[item].children);
                }
            }
            this.roleIds = nodes;
        }
    }
})
